package com.tiger.project.service.impl;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.tiger.common.core.redis.RedisCache;
import com.tiger.common.utils.Code;
import com.tiger.common.utils.DateUtils;
import com.tiger.common.utils.StringUtils;
import com.tiger.project.domain.*;
import com.tiger.project.domain.req.MaintenanceApplyBack;
import com.tiger.project.mapper.*;
import com.tiger.project.service.IMaintenanceApplyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【维修项目】Service业务层处理
 *
 * @author standard
 * @date 2022-09-16
 */
@Service
public class MaintenanceApplyServiceImpl implements IMaintenanceApplyService {
    @Autowired
    private MaintenanceApplyMapper maintenanceApplyMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private AssetMapper assetMapper;
    @Autowired
    private MaintenanceEnclosureMapper enclosureMapper;
    @Autowired
    private PatrolRecordMapper patrolRecordMapper;
    @Autowired
    private ProjectArchiveMapper projectArchiveMapper;

    /**
     * 查询【维修项目】
     *
     * @param id 【维修项目】主键
     * @return 【维修项目】
     */
    @Override
    public MaintenanceApply selectMaintenanceApplyById(Long id) {
        MaintenanceApply maintenanceApply = maintenanceApplyMapper.selectMaintenanceApplyById(id);

        // 资产
        Asset asset = new Asset();
        asset.setId(maintenanceApply.getAssetId());
        List<Asset> list = assetMapper.selectAssetList(asset);
        if (list != null && list.size() > 0){
            maintenanceApply.getAssetList().addAll(list);
        }

        //附件
        MaintenanceEnclosure enclosure = new MaintenanceEnclosure();
        enclosure.setMaintenanceApplyId(id);
        List<MaintenanceEnclosure> maintenanceEnclosureList = enclosureMapper.selectMaintenanceEnclosureList(enclosure);
        if (maintenanceEnclosureList !=null && maintenanceEnclosureList.size() > 0){
            maintenanceApply.getEnclosureList().addAll(maintenanceEnclosureList);
        }

        //巡查单号
        if (maintenanceApply.getPatrolRecordId() != null){
            PatrolRecord patrolRecord = patrolRecordMapper.selectPatrolRecordById(maintenanceApply.getPatrolRecordId());
            maintenanceApply.setPatrolRecordNo(patrolRecord.getPatrolRecordNo());
        }

        //维修项目图片
        if (StringUtils.isNotEmpty(maintenanceApply.getMaintenancePic())){
            maintenanceApply.setMaintenancePicList(Arrays.asList(maintenanceApply.getMaintenancePic().split(",")));
        }



        return maintenanceApply;
    }

    /**
     * 查询【维修项目】列表
     *
     * @param maintenanceApply 【维修项目】
     * @return 【维修项目】
     */
    @Override
    public List<MaintenanceApply> selectMaintenanceApplyList(MaintenanceApply maintenanceApply) {
        return maintenanceApplyMapper.selectMaintenanceApplyList(maintenanceApply);
    }

    /**
     * 新增【维修项目】
     *
     * @param maintenanceApply 【维修项目】
     * @return 结果
     */
    @Override
    public int insertMaintenanceApply(MaintenanceApply maintenanceApply) {
        int code = redisCache.getCacheObjectCode(LocalDate.now().format(Code.fmt) + "wx");
        addCode(code);
        maintenanceApply.setMaintenanceApplyNo(String.format("%s%s%04d", "WX",LocalDate.now().format(Code.fmt), code));
        maintenanceApply.setCreatedAt(DateUtils.getNowDate());
        maintenanceApply.setIsDeleted(0L);
        maintenanceApply.setStatus(1L);
        int i = maintenanceApplyMapper.insertMaintenanceApply(maintenanceApply);
        change(maintenanceApply);
        return i;
    }

    /**
     * 修改【维修项目】
     *
     * @param maintenanceApply 【维修项目】
     * @return 结果
     */
    @Override
    public int updateMaintenanceApply(MaintenanceApply maintenanceApply) {
        change(maintenanceApply);
        maintenanceApply.setUpdatedAt(DateUtils.getNowDate());
        return maintenanceApplyMapper.updateMaintenanceApply(maintenanceApply);
    }

    /**
     * 批量删除【维修项目】
     *
     * @param ids 需要删除的【维修项目】主键
     * @return 结果
     */
    @Override
    public int deleteMaintenanceApplyByIds(Long[] ids) {
        return maintenanceApplyMapper.deleteMaintenanceApplyByIds(ids);
    }

    /**
     * 删除【维修项目】信息
     *
     * @param id 【维修项目】主键
     * @return 结果
     */
    @Override
    public int deleteMaintenanceApplyById(Long id) {
        return maintenanceApplyMapper.deleteMaintenanceApplyById(id);
    }

    //审核回调
    @Override
    public int callBack(MaintenanceApplyBack maintenanceApplyBack) {
        MaintenanceApply maintenanceApply = maintenanceApplyMapper.selectMaintenanceApplyById(maintenanceApplyBack.getId());
        BeanUtils.copyProperties(maintenanceApplyBack,maintenanceApply);
        //审核通过,项目可归档
        if (maintenanceApply.getStatus()==3L){
            ProjectArchive archive = new ProjectArchive();
            archive.setProjectId(maintenanceApply.getId());
            archive.setProjectNo(maintenanceApply.getMaintenanceApplyNo());
            archive.setProjectType(1L);
            archive.setCreatedAt(DateUtils.getNowDate());
            archive.setIsDeleted(0L);
            archive.setAssetName(maintenanceApply.getAssetName());
            archive.setTenantryId(maintenanceApply.getTenantryId());
            archive.setTenantryName(maintenanceApply.getTenantryName());
            archive.setTenantryPhone(maintenanceApply.getTenantryPhone());
            archive.setProjectDate(maintenanceApply.getCreatedAt());
            archive.setIsArchived(0L);
            archive.setCompanyId(maintenanceApply.getCompanyId());
            projectArchiveMapper.insertProjectArchive(archive);
        }
        maintenanceApply.setUpdatedAt(DateUtils.getNowDate());
        return maintenanceApplyMapper.updateMaintenanceApply(maintenanceApply);
    }


    /**
     * 生成编码
     */
    private synchronized void addCode(int code) {
        redisCache.setCacheObject(LocalDate.now().format(Code.fmt) + "wx", code + 1);
    }

    private void change(MaintenanceApply maintenanceApply){
        enclosureMapper.deleteMaintenanceEnclosureByMainId(maintenanceApply.getId());
        if (StringUtils.isNotEmpty(maintenanceApply.getEnclosureList())){
            maintenanceApply.getEnclosureList().forEach(item ->{
                item.setCreatedAt(DateUtils.getNowDate());
                item.setState("1");//上传成功
                item.setMaintenanceApplyId(maintenanceApply.getId());
                enclosureMapper.insertMaintenanceEnclosure(item);
            });
        }
    }


}
